home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!usenet
- From: GHouck <hksys@teleport.com>
- Newsgroups: comp.lang.c
- Subject: Re: double-->float... number changes
- Date: 20 Apr 1996 05:55:19 GMT
- Organization: systems hk
- Message-ID: <4l9u87$sq@nadine.teleport.com>
- References: <4l3olr$85o@news.magi.com>
- NNTP-Posting-Host: ip-pdx18-35.teleport.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- mlefebvr@magi.com (Michel L.) wrote:
- >I have a number (that I pick up from Sybase), declared as a double,
- >it's value is 123.4, I then want to store it in a float variable, but
- >the number changes to 123.399994.
- >
- >float flt_number=0;
- >double dbl_number=123.4;
- >flt_number = dbl_number;
- >
- >flt_number is 123.399994.
- >
- >This messes up calculations later on in program.
- >I tried converting the number to a string and using atof() to get a
- >float number, but this function returns a double?!, and the subsequent
- >conversion to a float produces the same error.
- >
- >Is there a way to get 123.4 when converting to a float number?
- >
-
- Michel,
-
- Since floats and doubles typically allow 7 and 15 significant digits
- respectively, why would you want to convert the numbers to float
- for '...calculations later on in the program...'? Would it not
- be more reasonable to maintain the numbers in the form that carries
- the greatest precision until the final print/output? With each
- operation, your lessened precision would accumulate a greater total
- error (perhaps). It appears float 123.4, when printed, because it
- is promoted to double in the function call, cannot be reasonably
- represented in the 9 digits you show, since it was probably only
- precise to 7 of them to begin with (approximately).
-
- Yours, Geoff Houck
-
-